package com.mspbots.core.config;

import com.mspbots.core.system.service.SystemConfigurationService;
import com.mspbots.core.wise.model.Tenant;
import com.mspbots.core.wise.service.TenantConfigurationService;
import com.mspbots.core.wise.service.TenantRuleService;
import com.mspbots.core.wise.service.TenantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * com.mspbots.core.config.CachingConfiguration
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/10/14
 */
@Slf4j
@Configuration
@EnableCaching
@RequiredArgsConstructor
public class CachingConfiguration {

    public static final String REDIS_KEY_PREFIX_SYSTEM = "MSPBOTS:CONFIG:SYSTEM:";
    public static final String REDIS_KEY_PREFIX_TENANT = "MSPBOTS:CONFIG:TENANT:";

    private final SystemConfigurationService systemConfigurationService;
    private final TenantConfigurationService tenantConfigurationService;
    private final TenantService tenantService;
    private final TenantRuleService tenantRuleService;
    private final StringRedisTemplate stringRedisTemplate;
    private final RedisTemplate<String, Object> redisTemplate;

    @Bean
    public CommandLineRunner start() {
        return args -> {
            systemConfigurationService.list().parallelStream().forEach(dto -> stringRedisTemplate.opsForValue()
                    .set(REDIS_KEY_PREFIX_SYSTEM + dto.getKeyword(), dto.getKeywordValue()));
            List<Tenant> tenantList = tenantService.list();
            tenantList.parallelStream().forEach(tenant -> {
                tenantConfigurationService.selectAllByTenantId(tenant.getId())
                        .parallelStream()
                        .forEach(tenantConfiguration -> stringRedisTemplate.opsForValue()
                                .set(REDIS_KEY_PREFIX_TENANT + tenantConfiguration.getTenantId()
                                        + ":" + tenantConfiguration.getKeyword(), tenantConfiguration.getKeywordValue())
                        );
                /*tenantRuleService.selectByTenantId(tenant.getId()).parallelStream().forEach(rule -> {
                    stringRedisTemplate.opsForValue().set(REDIS_KEY_PREFIX_TENANT + rule.getTenantId() + ":"
                            + rule.getRuleId() + "_sending", String.valueOf(rule.getEnable()));
                    stringRedisTemplate.opsForValue().set(REDIS_KEY_PREFIX_TENANT + rule.getTenantId() + ":"
                            + rule.getRuleId() + "_enable", String.valueOf(rule.getEnable()));
                });*/
            });
            this.redisTemplate.delete(REDIS_KEY_PREFIX_TENANT + "tenants");
            this.redisTemplate.opsForList().leftPushAll(REDIS_KEY_PREFIX_TENANT + "tenants", Arrays.asList(tenantList.toArray()));
        };
    }

}
